# -*- coding: utf-8 -*-
import sys

from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QLCDNumber
from core import SerialThread
from gui import Demo_ui
from model import Player, Serial

class Rusher(Demo_ui):
    def __init__(self):
        super(Rusher, self).__init__()  

        self.red_player = Player(name = "red")
        self.red_player.time_chage.connect(self.red_slot)
        self.blue_player = Player(name = "blue")
        self.blue_player.time_chage.connect(self.blue_slot)
        self.current_player = None
        self.use_set = False
        self.user_set_time = None

        #初始化
        self.initUI()
        
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.timer_slot)
        
        self.init()

    #初始化函数，绑定槽函数，启动弹窗提示
    def init(self):
        self.bnt_setup.clicked.connect(self.do_bnt_setup)
        self.bnt_start.clicked.connect(self.do_bnt_start)
        self.bnt_stop.clicked.connect(self.do_bnt_reset)
        self.pop_check_win()

    #弹窗提示,连接串口
    def pop_check_win(self):
        #检测USB端口
        port_list = Serial.serial_check()
        if len(port_list) != 0:
            list_p = []
            for i in range(0,len(port_list)):                
                list_p.append(str(port_list[i]))
            print(list_p)
            value, result = QtWidgets.QInputDialog.getItem(self, "选择串口", "请选择串口\n\n", list_p, 1, True) 
            if result:
                print(value[0:4])
                ser = Serial(port = value[0:4])
                self.ser_thread = SerialThread(ser, self)

                if self.ser_thread.ser.is_open:
                    print("串口连接成功！")
                    # self.ser_thread.start()
                    return False
                else:
                    return True

            else:
                #退出程序
                pass
        else:
            reply = QtWidgets.QMessageBox.information(self,
                                    "未找到可用串口",  
                                    "请通过USB接入抢答器！")

    #设置按钮
    def do_bnt_setup(self):
        
        value, ok = QtWidgets.QInputDialog.getInt(self,"倒计时设置", "请设置倒计时(秒)\n\n请输入整数:", 100, 0, 5000, 2)
        self.red_player.set_time(value * 10)
        self.blue_player.set_time(value * 10)
        self.use_set = True
        self.user_set_time = value * 10
        self.downRight.setStyleSheet("background:#FFFFFF")
        self.downLeft.setStyleSheet("background:#FFFFFF")
    #开始按钮
    def do_bnt_start(self):
        self.ser_thread.start()
        self.timer.start(100)
    
    #暂停按钮
    def do_bnt_reset(self):
        self.ser_thread.stop()
        self.timer.stop()
         

        if self.use_set is True:
            self.red_player.set_time(self.user_set_time)
            self.blue_player.set_time(self.user_set_time)
            self.downRight.setStyleSheet("background:#FFFFFF")
            self.downLeft.setStyleSheet("background:#FFFFFF") 
        else:
            self.red_player.set_time(100)
            self.blue_player.set_time(100)
            self.downRight.setStyleSheet("background:#FFFFFF")
            self.downLeft.setStyleSheet("background:#FFFFFF") 

    #槽函数
    def red_slot(self,value):
        if value >= 0:
            value = self.int_2_str(value)
            self.downRight.setStyleSheet("background:#FFFFFF")
            self.downLeft.display(value.center(6))        
            self.downLeft.setStyleSheet("background:#f69c9f")
        else:
            pass
        

    def blue_slot(self,value):
        if value >= 0:
            value = self.int_2_str(value)
            self.downLeft.setStyleSheet("background:#FFFFFF")      
            self.downRight.display(value.center(6))
            self.downRight.setStyleSheet("background:#426ab3")
        else:
            pass

    def timer_slot(self):
        if self.current_player == None:
            return
        self.current_player.down_time()

    def int_2_str(self,value):
        val_2_list = list(str(value))
        val_2_list.insert(len(str(value))- 1,".")
        value = ''.join(val_2_list)
        if len(str(value)) == 0:
            value = str("00")
        return str(value)

  

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    demo = Rusher()
    demo.show()
    sys.exit(app.exec_())

